package com.changhai.diabetes.service.impl;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.changhai.diabetes.model.SportsSuifang;
import com.changhai.diabetes.model.SportsSurvey;
import com.changhai.diabetes.model.SportsTreatment;
import com.changhai.diabetes.service.SportService;
import com.changhai.diabetes.util.Constants;

/**
 * 运动
 * 
 * @author Andy
 * 
 */
public class SportServiceImpl extends CommonServiceImpl implements SportService {

	public SportsSurvey searchSurveyByPatientId(Long patientId){	 
		Object o = this.getObject(Constants.DS_CHANGHAI, "sportsSurvey.getByPatientId", patientId);	 
		if (o != null && o instanceof SportsSurvey) {
			return (SportsSurvey) o;
		} else {
			SportsSurvey s = new SportsSurvey();
			s.setPatientid(patientId);
			return s;
		}
	}
	
	public void saveEditSportsSurvey(SportsSurvey sportsSurvey){
		if (sportsSurvey != null) {
			if(sportsSurvey.getSportssurveyid()!=null){
				this.updateObject(Constants.DS_CHANGHAI, "sportsSurvey.updateByPrimaryKey", sportsSurvey);
			}else{
				Long id = this.saveNewSportsSurvey(sportsSurvey);
				if (id != null && id > 0) {
					sportsSurvey.setSportssurveyid(id);
					this.updateObject(Constants.DS_CHANGHAI, "sportsSurvey.updateByPrimaryKey", sportsSurvey);
				}
			}
		}
	}
	
	public Long saveNewSportsSurvey(SportsSurvey sportsSurvey) {
		Integer id = 0;
		if (sportsSurvey != null && sportsSurvey.getPatientid()!=null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "sportsSurvey.insert", sportsSurvey);
		}
		return new Long(id);
	}
	
	public SportsTreatment searchTreatByPatientId(Long patientId){	 
		Object o = this.getObject(Constants.DS_CHANGHAI, "sportsTreatment.getByPatientId", patientId);	 
		if (o != null && o instanceof SportsTreatment) {
			return (SportsTreatment) o;
		} else {
			SportsTreatment t = new SportsTreatment();
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			t.setPatientid(patientId);
			t.setStarttime(df.format(new Date()));
			return t;
		}
	}
	
	public SportsSuifang searchSuifangByPatientId(Long patientId){	 
		Object o = this.getObject(Constants.DS_CHANGHAI, "sportsSuifang.getByPatientId", patientId);	 
		if (o != null && o instanceof SportsSuifang) {
			return (SportsSuifang) o;
		} else {
			SportsSuifang t = new SportsSuifang();
			t.setPatientid(patientId);
			return t;
		}
	}
	
	public void saveEditSportsTreat(SportsTreatment sportsTreat){
		if (sportsTreat != null) {
			if(sportsTreat.getSeqid()!=null){
				this.updateObject(Constants.DS_CHANGHAI, "sportsTreatment.updateByPrimaryKey", sportsTreat);
			}else{
				Long id = this.saveNewSportsTreat(sportsTreat);
				if (id != null && id > 0) {
					sportsTreat.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "sportsTreatment.updateByPrimaryKey", sportsTreat);
				}
			}
		}
	}
	
	public void saveEditSportsSuifang(SportsSuifang sportsSuifang){
		if (sportsSuifang != null) {
			if(sportsSuifang.getSportstreatid()!=null){
				this.updateObject(Constants.DS_CHANGHAI, "sportsSuifang.updateByPrimaryKey", sportsSuifang);
			}else{
				Long id = this.saveNewSportsSuifang(sportsSuifang);
				if (id != null && id > 0) {
					sportsSuifang.setSportstreatid(id);
					this.updateObject(Constants.DS_CHANGHAI, "sportsSuifang.updateByPrimaryKey", sportsSuifang);
				}
			}
		}
	}
	
	public Long saveNewSportsTreat(SportsTreatment sportsTreat) {
		Integer id = 0;
		if (sportsTreat != null && sportsTreat.getPatientid()!=null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "sportsTreatment.insert", sportsTreat);
		}
		return new Long(id);
	}
	
	public Long saveNewSportsSuifang(SportsSuifang sportsSuifang) {
		Integer id = 0;
		if (sportsSuifang != null && sportsSuifang.getPatientid()!=null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "sportsSuifang.insert", sportsSuifang);
		}
		return new Long(id);
	}
}
